package com.bailiban.controller;

import com.bailiban.service.YesterDayService;
import com.bailiban.utils.Result;
import com.bailiban.utils.ResultCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author zhulang
 * @Classname YesterdayIncresedController
 * @Description TODO
 * @Date 2020/3/25 15:34
 */
@RestController
@RequestMapping("/yesterdayIncresed")
@Api(tags = "昨日新增疫情控制器")
public class YesterdayIncresedController {
    @Autowired
    private YesterDayService yesterDayService;

    @ApiOperation(value = "查询指定日期和身份的昨日新增数据", notes = "昨日新增")
    @GetMapping("/all/date/provinceId/{date}/{provinceId}")
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "provinceId", value = "1"),
                    @ApiImplicitParam(name = "date", value = "yyyy-MM-dd")
            }
    )
    public Result findAllByDateAndProvinceId(@PathVariable("provinceId") Integer provinceId, @PathVariable("date") String date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, -24);
        System.out.println("date:" + date);
        if (StringUtils.isEmpty(date)) {
//            如果用户传递的日期为空
            date = simpleDateFormat.format(calendar.getTime());
        }
        Date parse = null;
        try {
            parse = simpleDateFormat.parse(date);
            System.out.println("parse" + parse);
        } catch (ParseException e) {
            e.printStackTrace();
            return Result.failure(ResultCode.PARAM_TYPE_BIND_ERROR);
        }
        return Result.success(yesterDayService.findYesterDayByProvinceIdAndDate(parse, provinceId));
    }
}
